package com.ytjj.qmyx.supplychain.service;

import com.ytjj.qmyx.supplychain.mapper.BankerProductSkuKeyMapper;
import com.ytjj.qmyx.supplychain.mapper.ProductAttributeKeyMapper;
import com.ytjj.qmyx.supplychain.mapper.ProductAttributeValueMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBankerProductSkuKey;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBankerProductSkuValue;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProductAttributeKey;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProductAttributeValue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ProductAttributeValueService {
    @Resource
    private ProductAttributeValueMapper productAttributeValueMapper;
    @Resource
    private ProductAttributeKeyMapper productAttributeKeyMapper;
    @Autowired
    private BankerProductSkuKeyService bankerProductSkuKeyService;
    @Autowired
    private BankerProductSkuValueService bankerProductSkuValueService;

    public Map<String, Object> getSkuKeysbyId(Integer id) {

        Map<String, Object> map = new HashMap<>();
        // 查询商品选择的属性 key 集合
        List<YxBankerProductSkuKey> bankerProductSkuKeyList = bankerProductSkuKeyService.getBankerProductSkuKeysByPid(id);
        // 查询商品选择的属性 value 集合
        List<YxBankerProductSkuValue> bankerProductSkuValueList = bankerProductSkuValueService.getBankerProductSkuValue(id);
        List<Integer> checkedIds = bankerProductSkuValueList.stream().map(YxBankerProductSkuValue::getVid)
                .collect(Collectors.toList());

        // 查询选择的属性key的所有value集合
        List<Map<String, Object>> specArray = new ArrayList<>();
        if (!CollectionUtils.isEmpty(bankerProductSkuKeyList)) {
            List<Integer> attributeKeyIds = bankerProductSkuKeyList
                    .stream()
                    .map(YxBankerProductSkuKey::getKid)
                    .collect(Collectors.toList());
            for (Integer attributeKeyId : attributeKeyIds) {
                Map<String, Object> specMap = new LinkedHashMap<>();
                Map<String, Object> spanKeysMap = new LinkedHashMap<>();
                YxProductAttributeKey productAttributeKey = productAttributeKeyMapper.selectByPrimaryKey(attributeKeyId);

                Example example = new Example(YxProductAttributeValue.class);
                example.createCriteria().andEqualTo("attributeId", productAttributeKey.getId());
                List<YxProductAttributeValue> productAttributeValues = productAttributeValueMapper.selectByExample(example);
                spanKeysMap.put("id", productAttributeKey.getId());
                spanKeysMap.put("name", productAttributeKey.getAttributeName());
                specMap.put("spanName", spanKeysMap);
                if (!CollectionUtils.isEmpty(productAttributeValues)) {
                    List<Map<String, Object>> spanValues = new ArrayList<>();
                    for (YxProductAttributeValue productAttributeValue : productAttributeValues) {
                        Map<String, Object> spanValuesMap = new HashMap<>();
                        spanValuesMap.put("id", productAttributeValue.getId());
                        spanValuesMap.put("name", productAttributeValue.getAttributeValue());
                        spanValues.add(spanValuesMap);
                    }
                    specMap.put("spanValue", spanValues);
                }
                specArray.add(specMap);
            }
        }
        map.put("selecteds", checkedIds);
        map.put("keys", specArray);
        return map;
    }
}
